package com.hse.gui.ClientComponents;

import com.hse.gui.IClientComponent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Nosov Alexander
 * Date: 02.06.2009
 * Time: 13:24:04
 * To change this template use File | Settings | File Templates.
 */


public class TempSwitcherComponent extends JPanel implements IClientComponent {
    private JPanel manualPanel;
    private JPanel autoPanel;
    private JRadioButton onButton;
    private ButtonGroup manualGroup;
    private JRadioButton offButton;
    private JTextField maxField;
    private JLabel maxLabel;
    private JButton updateSettings;


    private boolean manual;
    private boolean on;
    private ComboBoxModel guiEndDevices;
    private Object guiEndDevice;
    private int max;
    private int currentTemperature;

    public TempSwitcherComponent() {
        super(new GridLayout(2, 1));
        //JPanel manualORautoPanel=new JPanel();
        //add(manualORautoPanel);

        manualPanel = new JPanel();
        add(manualPanel);
        autoPanel = new JPanel();
        add(autoPanel);


        ButtonGroup manualORautoGroup = new ButtonGroup();
        JRadioButton manual = new JRadioButton("Manual", true);
        manual.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setManual(true);
            }
        });
        manualORautoGroup.add(manual);
        manualPanel.add(manual);
        JRadioButton auto = new JRadioButton("Auto", false);
        auto.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setManual(false);
            }
        });
        manualORautoGroup.add(auto);
        autoPanel.add(auto);


        manualGroup = new ButtonGroup();
        onButton = new JRadioButton("ON", false);
        manualGroup.add(onButton);
        manualPanel.add(onButton);
        offButton = new JRadioButton("OFF", true);
        manualGroup.add(offButton);
        manualPanel.add(offButton);


        maxField = new JTextField(2);
        maxLabel = new JLabel("Max:");
        autoPanel.add(maxLabel);
        autoPanel.add(maxField);
        updateSettings = new JButton("Update Settings");
        updateSettings.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    max = Integer.parseInt(maxField.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "Please, insert real numbers");
                }

            }
        });
        autoPanel.add(updateSettings);
        setManual(true);
    }

    public void setManual(boolean manual) {
        if (manual) {
            onButton.setEnabled(true);
            offButton.setEnabled(true);


            maxField.setEnabled(false);
            maxLabel.setEnabled(false);
        } else {

            onButton.setEnabled(false);
            offButton.setEnabled(false);

            maxField.setEnabled(true);
            maxLabel.setEnabled(true);
        }
        this.manual = manual;
    }


    public void recv(byte[] data) {
        String currentTemperatureString = new String(data);
        currentTemperature = Integer.valueOf(currentTemperatureString);
    }

    public byte[] send() {
        boolean switchON = false;
        if (manual) {
            if (onButton.isSelected()) {
                switchON = true;
            } else {
                switchON = false;
            }
        } else {
            if (currentTemperature < max) {
                switchON = true;
                onButton.setSelected(true);
                offButton.setSelected(false);
            } else {
                switchON = false;
                onButton.setSelected(false);
                offButton.setSelected(true);
            }
        }
        if (on != switchON) {
            on = switchON;
            if (switchON) {
                return "ON".getBytes();
            } else {
                return "OFF".getBytes();
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "Temperature Switcher";
    }
}
